﻿using System;

namespace AS
{
    class Program
    {
        static void Main(string[] args)
        {

            Dog dog = new Dog("dolly");
            var result = ConvertToSpecifiedObject<Person>(dog);
            if (result == null)
            {
                Console.WriteLine("Dog instance could not convert to Person instance.");
            }
            else
            {
                Console.WriteLine("Dog instance convert to Person instance success.");
            }

            Student student = new Student("jack", 18, "棉城中学");
            result = ConvertToSpecifiedObject<Person>(student);
            if (result == null)
            {
                Console.WriteLine("Student instance could not convert to Person instance.");
            }
            else
            {
                Console.WriteLine("Student instance convert to Person instance success.");
            }

        }

        public static T ConvertToSpecifiedObject<T>(object instance)where T:class
        {
            return instance as T;
        }
    }

    class Person
    {
        public string Name;
        public uint Age;
        public Person(string name, uint age)
        {
            Name = name;
            Age = age;
        }
        public override string ToString()
        {
            return String.Format("Person Name:{0}, Age:{1}", Name, Age);
        }
    }

    class Student : Person
    {
        public string School;
        public Student(string name, uint age, string school) : base(name, age)
        {
            School = school;
        }
        public override string ToString()
        {
            return String.Format("Student Name:{0}, Age:{1}, School:{2}", Name, Age, School);
        }
    }

    class Dog
    {
        public string Name;
        public Dog(string name)
        {
            Name = name;
        }
        public override string ToString()
        {
            return String.Format("Dog Name:{0}", Name);
        }
    }
}
